obj={"语文":33,"数学":100,"英语":90}
print(obj,obj["语文"])

# 元组作为字典的键
obj={(1,2):"语文",(3,4):"数学",(5,6):"英语"}
print(obj,obj[(1,2)])

# 添加字典
obj={"语文":33,"数学":100,"英语":90}
obj["物理"]=100
obj["语文"]=1 # 覆盖
print(obj)

# 删除字典
obj={"语文":33,"数学":100,"英语":90}
del obj["语文"]
print(obj)

# 判断,只判断键，不判断值
obj={"语文":33,"数学":100,"英语":90}
print("语文" in obj,"音乐" in obj,90 in obj)

# 元组长度
obj={"语文":33,"数学":100,"英语":90}
print(len(obj))


# 创建空字典
obj={}
print(obj)
obj["体育"]=None
# print(obj["数学"]) # 不存在，会报错
print(obj)

# get 方法
obj={"语文":33,"数学":100,"英语":90}
print(obj.get("数学"))
obj.clear() # 清空字典
print(obj.get("数学")) # 不存在，不会报错

# update 方法
obj={"语文":33,"数学":100,"英语":90}
obj.update({"物理":100,"数学":300}) # 存在则更新，不存在则添加
print(obj)

# items,keys,values 方法
obj={"语文":33,"数学":100,"英语":90}
print(obj.items())
print(type(obj.keys())) # type用于返回对象的类型
print(list(obj.values())[0])

# pop 方法 删除并返回
obj={"语文":33,"数学":100,"英语":90}
print(obj.pop("语文"))
print(obj)

# popitem 弹出并返回字典中的最后一个键值对（在Python 3.7+中）
obj={"语文":33,"数学":100,"英语":90}
print(obj.popitem())
print(obj)

# setdefault 方法 根据key获取对应值，当key不存在时，设置默认值并返回
obj={"语文":33,"数学":100,"英语":90}
print(obj.setdefault("语文",233))
print(obj)
print(obj.setdefault("历史",233))
print(obj.setdefault("体育")) # 未传第二个参数，赋值None
print(obj)

# formkeys 方法 使用给定的多个key创建字典,默认值为None,可传第二个参数覆盖默认值
keys=["语文","数学","英语"]
a_dict=dict.fromkeys(keys)
print(a_dict)
a_dict=dict.fromkeys(keys,33)
print(a_dict)
keys=("语文","数学","英语")
a_dict=dict.fromkeys(keys,338) # 支持元组
print(a_dict)

# 字曲格式化字符串
temp = "教程是:%(name)s,价格是:%(price)010.2f,出版社是:%(publisher)s"
book={"name":"Python","price":99.9,"publisher":"oreilly"}
print(temp % book)

# 遍历字典
obj={"语文":33,"数学":100,"英语":90}
for key in obj: # 遍历字典的key
    print(key,end=",")
print()
for value in obj.values(): # 遍历字典的value
    print(value,end=",")
print()
for key,value in obj.items(): # 遍历字典的key和value
    print(key,value,end=";")
print()

obj={}
obj[0,0]=33 # 元组作为字典的键
obj[0,1]=33
obj[0,2]=33
print(obj)
for key in obj: # 遍历字典的key
    if key[0]==key[1]:
        print (obj[key])
print(len(obj))

